package com.zksk.admin.controller.agreement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zksk.admin.api.RemoteFileService;
import com.zksk.admin.api.domain.SysFile;
import com.zksk.admin.domain.agreement.vo.AgreementVo;
import com.zksk.admin.service.customer.RAgreementCustomerReqService;
import com.zksk.admin.service.org.AgreementService;
import com.zksk.admin.service.org.RAgreementOrgProdService;
import com.zksk.common.core.constant.OrgConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.mbg.admin.domain.Agreement;
import com.zksk.mbg.admin.domain.RAgreementCustomerReq;
import com.zksk.mbg.admin.domain.RAgreementOrgProd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 协议管理 信息操作处理
 * @author zksk
 */

@Slf4j
@RestController
@RequestMapping("/agreement")
@Api(value = "AgreementController", tags = "协议相关接口")
public class AgreementController {

    @Autowired
    private AgreementService agreementService;
//    r_agreement_customer_req
    @Autowired
    private RAgreementOrgProdService orgProdService;

    @Autowired
    private RAgreementCustomerReqService reqService;

    @Autowired
    private RemoteFileService remoteFileService;


    /**
     * 协议上传
     * @param file
     * @return
     */
    @ApiOperation(value = "协议上传", notes = "协议上传")
    @PostMapping("/upload")
    @SneakyThrows
//    @HystrixCommand
    public R<SysFile> upload(@RequestPart("file") MultipartFile file) {
        R<SysFile> upload = remoteFileService.upload(file);
        return upload;
    }

    /**
     * 保存成功
     * @param vo
     * @return
     */
    @ApiOperation(value = "新增协议", notes = "新增")
    @PostMapping
    @SneakyThrows
    public R<Object> save(AgreementVo vo){
        agreementService.save(vo);
        Long agreementId = vo.getId();
        return R.ok(agreementId, "新增成功");
    }

    /**
     * 保存成功
     * @param vo
     * @return
     */
    @ApiOperation(value = "上传并保存协议文件", notes = "上传协议文件并保存")
    @PostMapping("/upload/save")
    @SneakyThrows
    public R<Object> uploadAndSave(AgreementVo vo, @RequestPart("file") MultipartFile file){
        log.info("创建一个模型");
        // 先进行资源文件上传
        R<SysFile> upload = remoteFileService.upload(file);
        String uploadPath = "";
        if (upload.getCode() == R.SUCCESS) {
            SysFile sysFile = upload.getData();
            uploadPath = sysFile.getUrl();
        }
        if(StringUtils.isNotBlank(uploadPath)){
            vo.setFileName(file.getName());
            vo.setTempFilePath(uploadPath);
        }
        log.info("上传文件开始");
        agreementService.save(vo);
        return R.ok(vo,"新增协议文件成功");
    }

    @ApiOperation(value = "获取协议列表根据机构产品id查询", notes = "获取协议列表根据机构产品id查询")
    @GetMapping("/getOrgAgreements")
    public R<List<Agreement>> getOrgAgreements(@RequestParam("orgProductId") Long orgProductId){
        List<RAgreementOrgProd> rAgreementOrgProds = orgProdService.list(new QueryWrapper<RAgreementOrgProd>()
                .eq(RAgreementOrgProd.ORG_PROD_ID, orgProductId)
                .eq(RAgreementOrgProd.ACTIVE, OrgConstants.ACTIVE_AV)
        );
        if (rAgreementOrgProds != null && rAgreementOrgProds.size() > 0) {
            List<Long> rOrgProdIds = rAgreementOrgProds.stream()
                    .map(RAgreementOrgProd::getAgreementId).collect(Collectors.toList());

            // 获取协议列表
            List<Agreement> list = agreementService.list(
                    new QueryWrapper<Agreement>()
                            .in(Agreement.ID, rOrgProdIds)
            );
            return R.ok(list, "机构产品关联得协议列表获取成功");
        }
        return R.fail("机构产品关联得协议列表获取失败");

    }

    @ApiOperation(value = "获取协议列表根据客户产品需求", notes = "获取协议列表根据客户产品需求")
    @GetMapping("/getCustomerAgreements")
    public R<List<Agreement>> getCustomerAgreements(@RequestParam("customerProdReqId") Long customerProdReqId){
        List<RAgreementCustomerReq> reqs = reqService.list(new QueryWrapper<RAgreementCustomerReq>()
                .eq(RAgreementCustomerReq.CUSTOMER_PROD_REQ_ID, customerProdReqId)
                .eq(RAgreementCustomerReq.ACTIVE, OrgConstants.ACTIVE_AV)
        );

        if (reqs != null && reqs.size() > 0) {
            List<Long> rOrgProdIds = reqs.stream()
                    .map(RAgreementCustomerReq::getAgreementId).collect(Collectors.toList());

            // 获取协议列表
            List<Agreement> list = agreementService.list(
                    new QueryWrapper<Agreement>()
                            .in(Agreement.ID, rOrgProdIds)
            );
            return R.ok(list, "客户需求产品关联得协议列表获取成功");
        }
        return R.fail("客户需求产品关联得协议列表获取失败");
    }

}
